//
//  BCMobileVerificationController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/26.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCMobileVerificationController.h"

#import "BCTitleInputCaptchaView.h"
#import "BCSureEnabledButton.h"

//
#import "BCPrictureCaptchaController.h"

//request
#import "BCUserWalletPhoneRequest.h"//界面数据请求
#import "BCUserPhoneRequest.h"//设置安全手机请求

@interface BCMobileVerificationController () <BCTitleInputCaptchaViewDelegate>

@property (nonatomic, strong) UIScrollView *contentScroll;

/**
 *BCTitleInputView
 */
@property (nonatomic, strong) BCTitleInputView *phoneView;

/**
 *codeView
 */
@property (nonatomic, strong) BCTitleInputCaptchaView *codeView;

/**
 *BCSureEnabledButton
 */
@property (nonatomic, strong) BCSureEnabledButton *sureButton;



#pragma mark - data

/**
 *BCUserWalletPhoneData
 */
@property (nonatomic, strong) BCUserWalletPhoneData *phoneData;

/**
 *vscode 验证码发送成功后返回的vscode
 */
@property (nonatomic, copy) NSString *vscode;

@end

@implementation BCMobileVerificationController

- (void)viewDidLoad {
    [super viewDidLoad];
    //
//    self.title = BCLocalizedKey(@"mobile_verify_title");
    [self setupSubviews];
    //
    [self requestWalletPhoneDataAction];
}
- (void)setupSubviews {
    self.contentScroll = [UIScrollView new];
    [self.view addSubview:self.contentScroll];
    [self.contentScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    [self setupContents];
    self.contentScroll.alpha = 0;
    [self.view layoutIfNeeded];
}
- (void)setupContents {
    CGFloat viewHeight = [BCTitleInputView defaultHeight];
    //
    self.phoneView = [BCTitleInputView new];
    self.phoneView.delegate = self;
    self.phoneView.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentScroll addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentScroll);
        make.left.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.height.mas_equalTo(viewHeight);
    }];
    //
    self.codeView = [BCTitleInputCaptchaView new];
    self.codeView.delegate = self;
    self.codeView.inputTextField.keyboardType = UIKeyboardTypeNumberPad;
    [self.contentScroll addSubview:self.codeView];
    [self.codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom);
        make.left.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll);
        make.height.mas_equalTo(viewHeight);
    }];
    //
    [self.codeView.captchaButton addTarget:self action:@selector(captchaButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    //
    self.sureButton = [BCSureEnabledButton new];
    [self.contentScroll addSubview:self.sureButton];
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.codeView.mas_bottom).offset(50.0);
        make.centerX.mas_equalTo(self.contentScroll);
        make.width.mas_equalTo(self.contentScroll).offset(-40.0);
        make.height.mas_equalTo(50.0);
    }];
    [self.sureButton addTarget:self action:@selector(sureButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - BCTitleInputCaptchaViewDelegate
- (void)titleInputView:(BCTitleInputView *)inputView textFieldTextDidChanged:(QMUITextField *)textField {
    //check
    NSString *account = self.phoneView.inputTextField.text;
    NSString *code = self.codeView.inputTextField.text;
    //
    //
    BOOL accountEnabled = account.length!=0;
    BOOL codeEnabled = code.length!=0;
    //
    BOOL sureEnabled = accountEnabled&&codeEnabled;
    //
    self.sureButton.enabled = sureEnabled;
}



#pragma mark - request
/**
 request
 */
- (void)requestWalletPhoneDataAction {
    [self.view bc_showLoadingInsets:UIEdgeInsetsMake(CGRectGetHeight(self.customNaviBar.frame), 0, 0, 0)];
    BCUserWalletPhoneRequest *request = [BCUserWalletPhoneRequest new];
    
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view bc_hiddenLoading];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserWalletPhoneResponse *resp = result;
        [wself handleWalletPhoneData:resp.data];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errorMsg = error.domain;
            [QMUITips showError:errorMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
        }
    }];
    [request requestServer];
}
- (void)handleWalletPhoneData:(BCUserWalletPhoneData *)phoneData {
    if (phoneData) {
        self.phoneData = phoneData;
        //
        self.contentScroll.alpha = 1;
        //title
        self.title = phoneData.flow.title;
        //设置phone
        self.phoneView.titleLabel.text = phoneData.flow.phone;
        self.phoneView.inputTextField.placeholder = phoneData.flow.phone_prompt;
        //设置code
        self.codeView.titleLabel.text = phoneData.flow.vcode;
        self.codeView.inputTextField.placeholder = phoneData.flow.vcode_prompt;
        self.codeView.captchaButton.sendTitle = phoneData.flow.vcode_submit;
        self.codeView.captchaButton.resendTitle = phoneData.flow.reacquire;
        self.codeView.captchaButton.captchaStauts = BCaptchaButtonStatusNormal;//设置验证码按钮状态
        //设置按钮
        [self.sureButton setTitle:phoneData.flow.submit forState:UIControlStateNormal];
    }
}

#pragma mark - actions
//发送验证码
- (void)captchaButtonDidClickedAction {
    //判断一下手机号码不为空就行了
    NSString *phone = self.phoneView.inputTextField.text;
    if (!phone.length) {
        [self.phoneView.inputTextField becomeFirstResponder];
        return;
    }
    [self.view endEditing:true];
    WEAK_SELF
    [BCPrictureCaptchaController showCaptcha:BCaptchaSendTypePhone account:phone title:self.phoneData.flow.image_code.title placeholder:self.phoneData.flow.image_code.input submitTitle:self.phoneData.flow.image_code.submit captchaLength:self.phoneData.flow.image_code.len fromController:self complete:^(NSString * _Nonnull pictureCaptcha) {
        wself.codeView.captchaButton.captchaStauts = BCaptchaButtonStatusCountdown;
        //
        wself.vscode = pictureCaptcha;
    }];
}


#pragma mark - sureButtonDidClickedAction
- (void)sureButtonDidClickedAction {
    //判断一下手机号码不为空就行了
    NSString *phone = self.phoneView.inputTextField.text;
    if (!phone.length) {
        [self.phoneView.inputTextField becomeFirstResponder];
        return;
    }
    if (!self.vscode.length) {
        [self captchaButtonDidClickedAction];
        return;
    }
    
    NSString *coder = self.codeView.inputTextField.text;
    if (!coder.length) {
        [self.codeView.inputTextField becomeFirstResponder];
        return;
    }
    [self.view endEditing:true];
    [self requestUserPhoneAction:phone vscode:self.vscode coder:coder];
}
/**
 请求绑定安全手机
 */
- (void)requestUserPhoneAction:(NSString *)phone vscode:(NSString *)vscode coder:(NSString *)coder {
    [self.view bc_showLoadingInsets:UIEdgeInsetsMake(CGRectGetHeight(self.customNaviBar.frame), 0, 0, 0)];
    BCUserPhoneRequest *request = [BCUserPhoneRequest new];
    request.phone = phone;
    request.vscode = vscode;
    request.coder = coder;
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view bc_hiddenLoading];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCUserPhoneResponse *resp = result;
        NSString *success = resp.msg;
        [QMUITips showSucceed:success inView:[UIApplication sharedApplication].keyWindow hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:success]];
        [wself.navigationController popViewControllerAnimated:true];
        [[NSNotificationCenter defaultCenter] postNotificationName:UserSafeCenterUpdateNotification object:nil];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error&&wself.view) {
            NSString *errorMsg = error.domain;
            [QMUITips showError:errorMsg inView:wself.view hideAfterDelay:[QMUITips smartDelaySecondsForTipsText:errorMsg]];
        }
    }];
    [request requestServer];
}


@end
